import { createRouter, createWebHistory } from 'vue-router';

// 创建路由实例
const minBangRouter = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/login",
      name: "login",
      component: () => import("./views/Login.vue"),
    },
    {
      path: "/",
      name: "admin",
      meta: {
        requireAuth: true,
      },
      component: () => import("./views/Admin.vue"),
      children: [
        {
          path: "user", 
          name: "user",
          component: () => import("./views/Admin/User.vue"),
        },
        {
          path: "news",
          name: "new",
          component: () => import("./views/Admin/News.vue"),
        },
        {
          path: "cases",
          name: "cases",
          component: () => import("./views/Admin/Cases.vue"),
        },
        {
          path: "team",
          name: "team",
          component: () => import("./views/Admin/Team.vue"),
        },
        {
          path: "course",
          name: "course",
          component: () => import("./views/Admin/Course.vue"),
        },
        {
          path: "enterprise",
          name: "enterprise",
          component: () => import("./views/Admin/Enterprise.vue"),
        },
        {
          path: "honor",
          name: "honor",
          component: () => import("./views/Admin/Honor.vue"),
        },
      ],
    },
  ],
});

minBangRouter.beforeEach((to, from, next) => {
  window.scrollTo(0, 0);
  if (to.matched.some((res) => res.meta.requireAuth)) {
    if (sessionStorage.getItem("token")) {
      next();
    } else {
      next({
        path: "/login",
        query: {
          redirect: to.fullPath,
        },
      });
    }
  } else {
    next();
  }
});

export default minBangRouter;